﻿using System;
using System.Collections.Generic;
using System.Reflection;
using Sirenix.OdinInspector;
using UnityEngine;

namespace Gp.AI {
    public enum AtkWeight {
        [LabelText("对1、2号位敌人")] FrontPos,
        [LabelText("对3、4号位敌人")] BackPos,
        [LabelText("对生命值 + 护甲最低敌人")] Vulnerable,
        [LabelText("对手持护盾敌人")] Shield,
        [LabelText("对处于措手不及等负面状态的敌人")] BeNegative,
        [LabelText("对缺少防御点的敌人（每点加权）")] LackDefence,
        [LabelText("没有格挡层数的敌人")] LackBlock,
        [LabelText("持有重武器的敌人")] HeavyWeapon,
    }

    [Serializable]
    public struct WeightInfo {
        [HideInInspector]
        public AtkWeight weight;
        [Title("@Label()")]
        [LabelText("权重")]
        public int num;
        public WeightInfo(AtkWeight weight, int num) {
            this.weight = weight;
            this.num = num;
        }
        
        
        #if UNITY_EDITOR
        private string Label() {
            return weight.Label();
        }
        #endif
    }


    public static class AtkWeightExtend {
        public static int DefaultValue(this AtkWeight weight) {
            return weight switch {
                AtkWeight.FrontPos => 4,
                AtkWeight.BackPos => 4,

                AtkWeight.Vulnerable => 8,
                AtkWeight.Shield => 8,

                AtkWeight.BeNegative => 4,
                AtkWeight.LackDefence => 2,
                AtkWeight.LackBlock => 4,

                AtkWeight.HeavyWeapon => 4,
                _ => throw new ArgumentOutOfRangeException(nameof(weight), weight, null)
            };
        }


        private static Dictionary<AtkWeight, string> _staticField = null;
        /// <summary>
        /// 仅用于编辑器拓展
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public static string Label(this AtkWeight field) {
            if (_staticField != null) return _staticField[field];
            
            _staticField = new Dictionary<AtkWeight, string>();
            Type t = typeof(AtkWeight);
            foreach (AtkWeight e in Enum.GetValues(t)) {
                string temp =
                    t.GetField(e.ToString()).GetCustomAttribute(typeof(LabelTextAttribute), false) is not
                        LabelTextAttribute
                        text
                        ? e.ToString()
                        : text.Text;
                _staticField[e] = temp;
            }

            return _staticField[field];
        }
    }
}